from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAdminUser
from datetime import date

from users.models import User

class UserTotalCount(APIView):
    # 权限指定
    permission_classes = [IsAdminUser]
    def get(self,request):
        """
            获取用户总数
        :param request:
        :return:
        """
        # 1、查询用户表获取所有注册用户数据
        count = User.objects.filter(is_staff=False).count()
        return Response({
            'count': count,
            'date': date.today()
        })


class UserDayCount(APIView):
    # 权限指定
    permission_classes = [IsAdminUser]

    def get(self, request):
        """
             获取日增用户
        :param request:
        :return:
        """
        # 1、获取当天日期
        now_date = date.today()
        # 2、根据日期查询注册用户
        count = User.objects.filter(date_joined__gte=now_date, is_staff=False).count()
        return Response({
            'count': count,
            'date': now_date
        })


class UserDayActiveCount(APIView):
    # 权限指定
    permission_classes = [IsAdminUser]

    def get(self, request):
        """
             获取日增用户
        :param request:
        :return:
        """
        # 1、获取当天日期
        now_date = date.today()
        # 2、根据日期查询登录用户
        count = User.objects.filter(last_login__gte=now_date, is_staff=False).count()
        return Response({
            'count': count,
            'date': now_date
        })


class UserDayOrdersCount(APIView):
    # 权限指定
    permission_classes = [IsAdminUser]

    def get(self, request):
        """
             获取日增用户
        :param request:
        :return:
        """
        # 1、获取当天日期
        now_date = date.today()
        # 2、根据日期查询下单用户 关联过滤查询 以订单表的信息作为过滤条件查询下单的用户信息
        count = User.objects.filter(orderinfo__create_time__gte=now_date, is_staff=False).count()
        return Response({
            'count': count,
            'date': now_date
        })